//
//  AppDelegate.m
//  FlingCurve
//
//  Created by Andrew de los Reyes on 3/27/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "AppDelegate.h"
#import "MachTimer.h"

#include <mach/mach.h>
#include <mach/mach_time.h>

@implementation AppDelegate

@synthesize window = _window;
@synthesize text_view = _text_view;

- (void)dealloc
{
    [super dealloc];
}

static NSMutableArray* myArr = nil;
static MachTimer* timer = nil;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    myArr = [[NSMutableArray alloc] init];
    timer = [[MachTimer alloc] init];
    // Insert code here to initialize your application
    for (size_t i = 0; i < 100; i++)
        [_text_view insertText:@"a\nb\nc\nd\ne\nf\ng\n"];
    [_text_view setPostsBoundsChangedNotifications:YES];
    [[[_text_view enclosingScrollView] contentView] setPostsBoundsChangedNotifications:YES];
    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(boundsChanged:)
                                                 name: NSViewBoundsDidChangeNotification
                                               object: _text_view];
    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(boundsChanged:)
                                                 name: NSViewBoundsDidChangeNotification
                                               object: [[_text_view enclosingScrollView] contentView]];
}

- (IBAction)clearLog:(id)sender
{
    [myArr removeAllObjects];
}
- (IBAction)dumpLog:(id)sender
{
    char* homedir = getenv("HOME");
    char path[1024];
    snprintf(path, sizeof(path), "%s/Desktop/scroll_dump.csv", homedir);
    
    int fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, 0644);
    if (fd < 0) {
        perror("open");
        return;
    }
    for (size_t i = 0; i < [myArr count]; i++) {
        NSArray* elt = [myArr objectAtIndex:i];
        float elapsed = [[elt objectAtIndex:0] floatValue];
        double location = [[elt objectAtIndex:1] doubleValue];
        char buf[1024];
        snprintf(buf, sizeof(buf), "%f,%f\n", elapsed, location);
        write(fd, buf, strlen(buf));
    }
    close(fd);
}


- (void)frameChanged:(NSNotification*)aNotification
{
    
    id obj = [aNotification object];
    if (obj == _text_view || obj == [_text_view enclosingScrollView] || 
        obj == [[_text_view enclosingScrollView] contentView]) {
        // change the layout of my subviews
        // (accessible via [self subviews])
        NSLog(@"Y: %f", [obj bounds].origin.y);
    }
    
}

- (void)boundsChanged:(NSNotification*)aNotification
{
    
    id obj = [aNotification object];
    if (obj == _text_view || obj == [_text_view enclosingScrollView] || 
        obj == [[_text_view enclosingScrollView] contentView]) {
        // change the layout of my subviews
        // (accessible via [self subviews])
        double location = [obj bounds].origin.y;
        float elapsed = [timer elapsedSec];
        NSArray* elt = [NSArray arrayWithObjects:[NSNumber numberWithFloat:elapsed], [NSNumber numberWithDouble:location], nil];
        [myArr addObject:elt];
    }
}

@end
